﻿using HtmlAgilityPack;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;

namespace HobSoft.SoccerBet2014.Domain.Common.Site.Component
{
    public class HtmlFormHelper
    {
        public class MyHtmlInputNode
        {
            public MyHtmlInputNode(HtmlNode node)
            {
                Node = node;
            }
            public string ID
            {
                get
                {
                    return Node.GetAttributeValue("id", "");
                }
            }
            public string Name
            {
                get
                {
                    return Node.GetAttributeValue("name", "");
                }
            }
            public string Value
            {
                get
                {
                    return Node.GetAttributeValue("value", "");
                }
                set
                {
                    Node.SetAttributeValue("value", value);
                }
            }
            protected HtmlNode Node { get; set; }
        }

        public List<MyHtmlInputNode> _inputNodes;
        public ReadOnlyCollection<MyHtmlInputNode> InputNodes
        {
            get;
            protected set;
        }

        public HtmlFormHelper(HtmlNodeCollection formNodes)
        {
            _inputNodes = new List<MyHtmlInputNode>();
            InputNodes = new ReadOnlyCollection<MyHtmlInputNode>(_inputNodes);
            foreach (HtmlNode node in formNodes)
            {
                _inputNodes.Add(new MyHtmlInputNode(node));
            }
        }

        public void ChangeValueByName(string name, string value)
        {
            var node = _inputNodes.FirstOrDefault(n => n.Name == name);
            if (node != null) node.Value = value;
        }

        public void ChangeValueByID(string id, string value)
        {
            var node = _inputNodes.FirstOrDefault(n => n.ID == id);
            if (node != null) node.Value = value;
        }

        public string GetValueByName(string name)
        {
            var node = _inputNodes.FirstOrDefault(n => n.Name == name);
            if (node == null) return string.Empty;
            return node.Value;
        }

        public string GetValueByID(string id)
        {
            var node = _inputNodes.FirstOrDefault(n => n.ID == id);
            if (node == null) return string.Empty;
            return node.Value;
        }

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            bool first = true;
            foreach (var item in InputNodes)
            {
                if (first)
                {
                    sb.AppendFormat("{0}={1}", item.Name, item.Value);
                    first = false;
                }
                else
                    sb.AppendFormat("&{0}={1}", item.Name, item.Value);
            }
            return sb.ToString();
        }
    }
}
